<?php

namespace App\Repositories\Home;

use App\Models\Admin\CategoryModel;
use App\Models\Admin\ComOptionModel;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCommentModel;
use App\Models\Admin\OrderModel;
use App\Models\Admin\UserModel;
use App\Models\AreaModel;
use App\Repositories\BaseRepository;

class CompanyRepository extends BaseRepository
{
    protected $repository;
    protected $option;
    protected $cate;
    protected $city;
    protected $user;
    protected $bale;
    protected $order;
    protected $orderComment;

    public function __construct()
    {
        $this->repository = new CompanyModel();
        $this->option = new ComOptionModel();
        $this->cate = new CategoryModel();
        $this->city = new AreaModel();
        $this->user = new UserModel();
        $this->bale = new OrderBaleModel();
        $this->order = new OrderModel();
        $this->orderComment = new OrderCommentModel();
    }

    /**
     * 获取web公司搜索条件
     */
    public function getWebConditions()
    {
        //公司类型
        $cateArr = [
            ['cateId'=>0, 'cateName'=>'所有',],
        ];
        $cates = $this->cate->where('del',0)
            ->where('pid',1)->get();
        if (count($cates)) {
            foreach ($cates as $cate) {
                $cateArr[] = [
                    'cateId' => $cate->id,
                    'cateName' => $cate->name,
                ];
            }
        }
        //公司地址
        $cityArr = [
            ['cityId'=>0, 'cityName'=>'所有',],
        ];
        $citys = $this->city->where('level',2)->get();
        if (count($citys)) {
            foreach ($citys as $city) {
                $cityArr[] = [
                    'cityId' => $city->id,
                    'cityName' => $city->name,
                ];
            }
        }
        //公司年份
        $yearArr = [
            ['yearId' =>0, 'yearName'=>'所有',],
            ['yearId' =>1, 'yearName'=>'1年内',],
            ['yearId' =>2, 'yearName'=>'3年内',],
            ['yearId' =>3, 'yearName'=>'5年内',],
            ['yearId' =>4, 'yearName'=>'5年以上',],
        ];
        return [
            'cateList' => $cateArr,
            'cityList' => $cityArr,
            'yearList' => $yearArr,
        ];
    }

    /**
     * 获取web公司列表
     * @param $cateId 工厂类型
     * @param $cityId 城市ID
     * @param $yearId 年份ID
     * @param $type 1公司或2工厂
     * @param $page 页码
     * @param $pageSize 每页记录数
     */
    public function getWebComList($cateId,$cityId,$yearId,$type,$page,$pageSize)
    {
        $query = $this->repository->where('shenhe','>',0);
        //搜索条件判断
        if ($cateId) { $query = $query->where('facType',$cateId); }
        if ($cityId) {
            //获取城市名称
            $cityOne = $this->city->where('id',$cityId)->first();
            $cityName = $cityOne ? $cityOne->sheort_name : '';
            $query = $query->where('comCity','like',$cityName);
        }
        if ($yearId) {
            //获取年份
            $yearCurr = date('Y',time());  //当前年份
            $dayCurr = date('mdHis',time());  //当前月份时间
            if ($yearId==1) {
                $year = $yearCurr - 1;
            } else if ($yearId==2) {
                $year = $yearCurr - 3;
            } else if ($yearId==3) {
                $year = $yearCurr - 5;
            } else {
                $year = $yearCurr - 6;
            }
            $yearTime = strtotime($year.$dayCurr);
            $query = $query->where('created_at','>',$yearTime);
        }
        //分页处理
        $limit = $pageSize ? $pageSize : $this->limit;
        $pageStart = ($page - 1) * $limit;
        $models = $query->skip($pageStart)->take($limit)->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $cateOne = $this->cate->where('id',$model->facType)->first();
            $yearName = date('Y',time()) - date('Y',$model->created_at).'年';
            $userOne = $this->user->where('id',$model->uid)->first();
            $facTypeName = $cateOne ? $cateOne->name.'类' : '未定义';
            $head = $userOne ? $userOne->head : '';
            $dataArr[] = [
                'comName' => $model->com_name,
                'facTypeName' => $facTypeName,
                'cityName' => $model->comCity,
                'yearName' => $yearName,
                'comLogo' => $head,
            ];
        }
        return $dataArr;
    }

    /**
     * 获取该公司发布的订单
     */
    public function getBaleList($uid,$page,$pageSize)
    {
        $query = $this->bale->where('del',0);
        //分页处理
        $limit = $pageSize ? $pageSize : $this->limit;
        $pageStart = ($page - 1) * $limit;
        $models = $query->where('uid',$uid)
            ->skip($pageStart)
            ->take($limit)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $dataArr[] = [
                'baleName' => $model->name,
                'baleImg' => $model->img,
            ];
        }
        return $dataArr;
    }

    /**
     * 获取该公司已合作成功的历史订单
     */
    public function getHistoryOrderList($uid,$page,$pageSize)
    {
        $query = $this->order->where('fid',$uid);
        //分页处理
        $limit = $pageSize ? $pageSize : $this->limit;
        $pageStart = ($page - 1) * $limit;
        $models = $query->where('status','>=',7)
            ->skip($pageStart)
            ->take($limit)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $baleOne = $this->bale->where('id',$model->bale)->first();
            $dataArr[] = [
                'baleName' => $baleOne ? $baleOne->name : '未定义',
                'baleImg' => $baleOne ? $baleOne->img : '',
                'orderNo' => $model->order_no,
            ];
        }
        return $dataArr;
    }

    /**
     * 获取公司概要:
     * 》公司名称、座机、传真、地址、描述;
     * 》公司坐标；
     * 》客户评价;
     */
    public function getAbout($uid)
    {
        //公司信息
        $model = $this->repository->where('uid',$uid)->first();
        $posJsons = $model ? json_decode($model->position,true) : '';
        //公司坐标
        $lng = $posJsons ? $posJsons['lng'] : '0';
        $lat = $posJsons ? $posJsons['lat'] : '0';
        //客户评价
        $orders = $this->order->where('fid',$uid)
            ->where('status','>=',7)->get();
        $orderNos = array();
        foreach ($orders as $order) {
            $orderNos[] = $order->order_no;
        }
        $comments = $this->orderComment->where('status',0)
            ->whereIn('out_trade_no',$orderNos)->get();
        $commentArr = array();
        foreach ($comments as $comment) {
            $comOne = $this->repository->where('uid',$comment->uid)->first();
            $levels = $this->orderComment->getLevels();
            $levelName = $levels[$comment->level];
            $commentArr[] = [
                'comName' => $comOne ? $comOne->com_name : '未定义',
                'orderNo' => $comment->out_trade_no,
                'content' => $comment->content,
                'level' => $model->level,
                'levelName' => $levelName,
            ];
        }
        return array(
            'comName' => $model->com_name,
            'comTel' => $model->com_tel,
            'comFax' => $model->com_fax,
            'comCity' => $model->comCity,
            'detail' => $model->detail,
            'lng' => $lng,
            'lat' => $lat,
            'comments' => $commentArr,
        );
    }
}